<?php
/**
 * @Author:      lyt
 * @DateTime:    2017-02-13 13:51:50
 * @Description: 用于测试自动打印的控制器
 */
use JonnyW\PhantomJs\Client;

class AutoprintController extends BaseController {
   
   /**
    * @Author:      lyt
    * @DateTime:    2017-02-13 13:52:34
    * @Description: 答应请求
    */
    public function prints(){
        
        $client = Client::getInstance();
        $client->getEngine()->setPath('/data/anhui/apiprint/bin/phantomjs');
        $request = $client->getMessageFactory()->createPdfRequest('http://192.168.20.247:3000/order/checkReportPrint?order_id=14866120299307', 'GET');
        $request->setOutputFile('/document1.pdf');
        $request->setFormat('A4');
        $request->setOrientation('landscape');
        $request->setMargin('1cm');
        $response = $client->getMessageFactory()->createResponse();
        $client->send($request, $response);

    }

    /**
     * @Author:      lyt
     * @DateTime:    2017-02-27 18:02:08
     * @Description: 自动打印接口
     */
    public function save_30_check_print(){
        $validator = Validator::make(Input::all(),
            array(
                'order_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            $order_id = Input::get('order_id'); //订单ID
            try {
                $order=Orders::find(Input::get("order_id"));
                if(is_null($order)){
                    $response = Lang::get("scs_error.order_notexiet_40000");
                    break;
                }               
                $uci=UserCarInfo::find($order->user_car_id);
                if(is_null($uci)&&$order->order_type==1){//服务单才判断
                    $response = Lang::get("scs_error.order_car_info_notexit_40001");
                    break;
                }
                $csi=DataCarStandardInfo::find($order->car_model_id);
                if(is_null($csi)&&$order->order_type==1){
                    $response = Lang::get("scs_error.order_car_model_notexit_40002");
                    break;
                }

                $o3c_pdf_url=Config::get('config.pdf_url')."order_id=".$order_id;
                $pdf_name= Common::getPdfName();
                // PDF::loadFile($o3c_pdf_url)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0)->save(Config::get('config.pdf_file').$pdf_name);
                // $pt=PrintTask::where('order_id',$order_id)->first();
                // if(!$pt){
                $client = Client::getInstance();
                $client->isLazy(); // Tells the client to wait for all resources before rendering
                $client->getEngine()->setPath(Config::get('config.phantomjs_uri'));
                $client->getEngine()->addOption('--output-encoding=UTF8');
                $request = $client->getMessageFactory()->createPdfRequest($o3c_pdf_url, 'GET');
                $request->setOutputFile(Config::get('config.pdf_file').$pdf_name);
                $request->setDelay(1);
                $request->setFormat('A4');
                $request->setOrientation('portrait');
                $request->setMargin('1mm');
                $response = $client->getMessageFactory()->createResponse();
                $client->send($request, $response);

                    $pt=new PrintTask;
                    $pt->order_id=$order_id;
                    $pt->shop_id=$order->shop_id;
                    $pt->url=$pdf_name;
                    $pt->save();
                // }

                $result = array(
                    'order_id' => $order_id
                );
                $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
            } catch (Exception $e) {
                Log::error("error", array("system" => "sce" ,"event" => "save_30_check", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("sce_error.order_cancel_error_1008");
                break;
            }
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "save_30_check_print", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-2-14 14:18:16
     * @Description: 获取打印列表
     */
    public function lists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do {

            //传入参数
            $shop_id = Input::get('shop_id');
            $limit = Input::get("limit", 10);
            $offset = Input::get("offset", 0);
            $pts = PrintTask::where('shop_id',Input::get('shop_id'));
            if(Input::get('status')!=''){
                $pts->where('print_status',Input::get('status'));
            }
            $count=$pts->count();
            $pts=$pts->skip($limit*($offset-1))->take($limit)->get();//{{URL::asset('app/car/'.$cm->car_pic)}}
            foreach ($pts as $_pts) {
                $_pts->url='/app/pdf/'.$_pts->url;
            }
            $result = array(
                'count' => $count,
                'print_task' => $pts
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        // Log::info("info", array("system" => "report" ,"event" => "lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-2-14 14:35:32
     * @Description: 修改状态
     */
    public function update(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'task_id' => 'required',
                'status' => 'required',
                'shop_id' => 'required'
            )
        );
        do {
    
            $pt=PrintTask::find(Input::get('task_id'));
            if(!$pt){
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }
            PrintTask::where('shop_id',Input::get('shop_id'))->where('id',Input::get('task_id'))->update(array('print_status'=>Input::get('status')));
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("task_id"=>Input::get('task_id')));
        } while (false);
        Log::info("info", array("system" => "report" ,"event" => "lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}